﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;
using System.IO;

namespace AutoEncodeDaemon
{
    public class AutoEncodeDBContext : AutoEncodeDatabase
    {
        public AutoEncodeDBContext() :
            base(Properties.Settings.Default.AutoEncodeDatabaseConnectionString)
        {
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        private JobStatusIndex ChangeTorrentJobStatus(TorrentJobs TorrentJob, JobStatusIndex NewStatus)
        {
            TorrentJob.JobStatusIndex = NewStatus;
            TorrentJob.UpdateTime = DateTime.Now;
            SubmitChanges();
            AutoEncodeLog.Log2File(String.Format("TorrentJob with id {0} has changed its status to \"{1}\"", TorrentJob.TorrentJobID, NewStatus.JobStatusName));
            return NewStatus;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public JobStatusIndex ChangeTorrentJobStatus(TorrentJobs TorrentJob, string NewStatus)
        {
            return ChangeTorrentJobStatus(TorrentJob, JobStatusIndex.First(x => x.JobStatusName == NewStatus));
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        private JobStatusIndex ChangeEncodeJobStatus(EncodeJobs EncodeJob, JobStatusIndex NewStatus)
        {
            EncodeJob.JobStatusIndex = NewStatus;
            EncodeJob.UpdateTime = DateTime.Now;
            // if (EncodeJob.TorrentJobs.EncodeJobs.Count(jobs => jobs.JobStatusIndex.JobStatusName == "New") != 0) ChangeTorrentJobStatus(EncodeJob.TorrentJobs, JobStatusIndex.First(x => x.JobStatusName == "New"));
            // if (EncodeJob.TorrentJobs.EncodeJobs.Count(jobs => jobs.JobStatusIndex.JobStatusName == "Wating") != 0) ChangeTorrentJobStatus(EncodeJob.TorrentJobs,JobStatusIndex.First(x => x.JobStatusName == "Wating"));
            // if (EncodeJob.TorrentJobs.EncodeJobs.Count(jobs => jobs.JobStatusIndex.JobStatusName == "Processing") != 0) ChangeTorrentJobStatus(EncodeJob.TorrentJobs,JobStatusIndex.First(x => x.JobStatusName == "Processing"));
            // if (EncodeJob.TorrentJobs.EncodeJobs.Count(jobs => jobs.JobStatusIndex.JobStatusName == "Failed") != 0) ChangeTorrentJobStatus(EncodeJob.TorrentJobs,JobStatusIndex.First(x => x.JobStatusName == "Failed"));
            // if (EncodeJob.TorrentJobs.EncodeJobs.Count(jobs => jobs.JobStatusIndex.JobStatusName != "Done") == 0) ChangeTorrentJobStatus(EncodeJob.TorrentJobs,JobStatusIndex.First(x => x.JobStatusName == "Done"));
            // if (EncodeJob.TorrentJobs.EncodeJobs.Count(jobs => jobs.JobStatusIndex.JobStatusName != "Skipped") == 0)ChangeTorrentJobStatus(EncodeJob.TorrentJobs,JobStatusIndex.First(x => x.JobStatusName == "Skipped"));
            SubmitChanges();
            AutoEncodeLog.Log2File(String.Format("EncodeJob with id {0} has changed its status to \"{1}\"", EncodeJob.EncodeJobID, NewStatus.JobStatusName));
            return NewStatus;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public JobStatusIndex ChangeEncodeJobStatus(EncodeJobs EncodeJob, string NewStatus)
        {
            return ChangeEncodeJobStatus(EncodeJob, JobStatusIndex.First(x => x.JobStatusName == NewStatus));
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        private JobStatusIndex ChangeReleaseJobStatus(ReleaseJobs ReleaseJob, JobStatusIndex NewStatus)
        {
            ReleaseJob.JobStatusIndex = NewStatus;
            ReleaseJob.UpdateTime = DateTime.Now;
            SubmitChanges();
            AutoEncodeLog.Log2File(String.Format("ReleaseJob with id {0} has changed its status to \"{1}\"", ReleaseJob.ReleaseJobID, NewStatus.JobStatusName));
            return NewStatus;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public JobStatusIndex ChangeReleaseJobStatus(ReleaseJobs ReleaseJob, string NewStatus)
        {
            return ChangeReleaseJobStatus(ReleaseJob, JobStatusIndex.First(x => x.JobStatusName == NewStatus));
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public TorrentJobs AddTorrentJob(string TorrentName, string TorrentPath)
        {
            return AddTorrentJob(TorrentName, TorrentPath, JobStatusIndex.First(x => x.JobStatusName == "New"));
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public TorrentJobs AddTorrentJob(string TorrentName, string TorrentPath, JobStatusIndex Status)
        {
            TorrentJobs Job=new TorrentJobs();
            Job.CreateTime = DateTime.Now;
            Job.UpdateTime = DateTime.Now;
            Job.JobStatusIndex = Status;
            Job.TorrentPath = TorrentPath;
            Job.TorrentTitle = TorrentName;
            TorrentJobs.InsertOnSubmit(Job);
            SubmitChanges();
            AutoEncodeLog.Log2File(String.Format("TorrentJob with name \"{0}\" has been added to database", TorrentName));
            return Job;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public EncodeJobs AddEncodeJob(TorrentJobs TorrentJob, string InputFilePath, string OutputFilePath, long TargetFileSize)
        {
            return AddEncodeJob(TorrentJob,InputFilePath,OutputFilePath,TargetFileSize,0,0,0,0,0,0,0,JobStatusIndex.First(x => x.JobStatusName == "New"));
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public EncodeJobs AddEncodeJob(TorrentJobs TorrentJob, string InputFilePath, string OutputFilePath, long TargetFileSize, int TargetAudioBitrate, int ResizeHight, int ResizeWidth, int CropLeft, int CropRight, int CropTop, int CropBottom, JobStatusIndex Status)
        {
            EncodeJobs Job = new EncodeJobs();
            Job.CreateTime = DateTime.Now;
            Job.UpdateTime = DateTime.Now;
            Job.TorrentJobs = TorrentJob;
            Job.InputFilePath = InputFilePath;
            Job.OutputFilePath = OutputFilePath;
            Job.TargetFileSize = TargetFileSize;
            Job.TargetAudioBitrate = TargetAudioBitrate;
            Job.ResizeHight = ResizeHight;
            Job.ResizeWidth = ResizeWidth;
            Job.CropLeft = CropLeft;
            Job.CropRight = CropRight;
            Job.CropTop = CropTop;
            Job.CropBottom = CropBottom;
            Job.JobStatusIndex = Status;
            EncodeJobs.InsertOnSubmit(Job);
            SubmitChanges();
            AutoEncodeLog.Log2File(String.Format("EncodeJob with file \"{0}\" has been added to database", InputFilePath));
            return Job;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public ReleaseJobs AddReleaseJob(CatalogIndex ReleaseCatalog, string ReleaseName, string SubTitle, string Url, string AudioLanguage, string Sub, string Author, string Description, string Language, string Poster, string CastImg, JobStatusIndex Status, string NfoTemplate, string DescriptionTemplate)
        {
            ReleaseJobs Job = new ReleaseJobs();
            Job.CreateTime = DateTime.Now;
            Job.UpdateTime = DateTime.Now;
            Job.CatalogIndex = ReleaseCatalog;
            Job.ReleaseName = ReleaseName;
            Job.SubTitle = SubTitle;
            Job.Url = Url;
            Job.AudioLanguage = AudioLanguage;
            Job.Sub = Sub;
            Job.Author = Author;
            Job.Description = Description;
            Job.Language = Language;
            Job.Poster = Poster;
            Job.CastImg = CastImg;
            Job.JobStatusIndex = Status;
            Job.NfoTemplate = NfoTemplate;
            Job.DescriptionTemplate = DescriptionTemplate;
            ReleaseJobs.InsertOnSubmit(Job);
            SubmitChanges();
            AutoEncodeLog.Log2File(String.Format("ReleaseJob with name \"{0}\" has been added to database", ReleaseName));
            Directory.CreateDirectory(Path.Combine(Properties.Settings.Default.ReleaseJobPath, Job.ReleaseName));
            return Job;
        }
    }
}
